﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using WebSite.Core;
using System.Globalization;

namespace WebSite.Server
{
    /// <summary>
    /// UpdateTeamImg 的摘要说明
    /// </summary>
    public class UpdateTeamImg : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest( HttpContext context )
        {
            context.Response.ContentType = "text/json";

            RetInfo response = RetInfoHelper.WebServiceReturn( delegate()
            {
                if (Project_Session.CurrentUser == null || Project_Session.CurrentUserBaby == null)
                {
                    throw new RuntimeException( 3, "请先完善宝宝资料" );
                }

                if (NullableHelper.CastValueType( Project_Session.CurrentUserBaby.ConfirmTeamId ) == 0)
                {
                    throw new RuntimeException( 10, "您的宝宝尚未组队" );
                }

                string srcImg = context.Request["img"];
                if (!srcImg.StartsWith( "/UploadImages/" ))
                {
                    throw new RuntimeException( 10, "参数错误" );
                }

                Maticsoft.BLL.CarrefourCover.Team teambll = new Maticsoft.BLL.CarrefourCover.Team();
                var teammodel = teambll.GetModel( NullableHelper.CastValueType( Project_Session.CurrentUserBaby.ConfirmTeamId ) );
                if (teammodel == null)
                {
                    throw new RuntimeException( 10, "组队不存在" );
                }

                string newTeamImg = TeamHelper.CombinOneTeamHead( srcImg );
                teammodel.TeamHeadImg = newTeamImg;
                teambll.Update( teammodel );

                return newTeamImg;
            });

            context.Response.Write( response.SerializeToString( Common.Serialization.SerializationType.Json ) );
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}